#include "ticketitemmodel.h"

#include "ticket.h"

#include <QtCore/QList>
#include <QtCore/QDebug>

BaseTicketItemModel::BaseTicketItemModel(QList<Ticket *> tickets, QObject *parent)
    : QAbstractListModel(parent), m_tickets(tickets)
{
}

BaseTicketItemModel::~BaseTicketItemModel()
{
    qDeleteAll(m_tickets);
}

int BaseTicketItemModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_tickets.count();
}

QVariant BaseTicketItemModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.model() != this)
        return QVariant();

    if (index.row() > m_tickets.count())
        return QVariant();

    Ticket *ticket = m_tickets.at(index.row());

    switch (role) {
    case Qt::DisplayRole:
        switch (index.column()) {
        case 0:
            return ticket->id();
        case 1:
            return ticket->fieldByName("summary");
        case 2:
            return ticket->fieldByName("status");
        case 3:
            return ticket->fieldByName("owner");
        case 4:
            return ticket->fieldByName("type");
        case 5:
            return ticket->fieldByName("priority");
        case 6:
            return ticket->created().toString();
        case 7:
            return ticket->fieldByName("reporter");
        }
    case Qt::ToolTipRole:
        return ticketToolTip(ticket);
    case Id:
        return ticket->id();
    case Priority:
        return ticket->fieldByName("priority");
    case Summary:
        return ticket->fieldByName("summary");
    case Reporter:
        return ticket->fieldByName("reporter");
    case Assignee:
        return ticket->fieldByName("owner");
    case Status:
        return ticket->fieldByName("status");
    case Type:
        return ticket->fieldByName("type");
    case Description:
        return ticket->fieldByName("description");
    case Created:
        return ticket->created();
    }

    return QVariant();
}

QVariant BaseTicketItemModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
        return QAbstractItemModel::headerData(section, orientation, role);

    QString title;
    switch(section) {
    case 0:
        title = tr("ID");
        break;
    case 1:
        title = tr("Summary");
        break;
    case 2:
        title = tr("Status");
        break;
    case 3:
        title = tr("Owner");
        break;
    case 4:
        title = tr("Type");
        break;
    case 5:
        title = tr("Priority");
        break;
    case 6:
        title = tr("Created");
        break;
    case 7:
        title = tr("Reporter");
        break;
    }
    return title;
}

Ticket *BaseTicketItemModel::ticketAtIndex(QModelIndex &index)
{
    if (!index.isValid() || index.model() != this)
        return 0;

    if (index.row() > m_tickets.count())
        return 0;

    return m_tickets.at(index.row());
}

int BaseTicketItemModel::columnCount(const QModelIndex &parent) const
{
    return (parent.column() > 0) ? 0 : 7;
}

void BaseTicketItemModel::addTicket(Ticket *ticket)
{
    beginInsertRows(QModelIndex(), m_tickets.count(), m_tickets.count());
    m_tickets << ticket;
    endInsertRows();
}

void BaseTicketItemModel::appendTickets(QList<Ticket *> newTickets)
{
    beginInsertRows(QModelIndex(), m_tickets.count(), m_tickets.count() + newTickets.count());
    m_tickets.append(newTickets);
    endInsertRows();
}

void BaseTicketItemModel::notifyQueryCompleted(int total, int done)
{
    emit queryCompleted(total, done);
}

QString BaseTicketItemModel::ticketToolTip(const Ticket *ticket) const
{
    QString res;
    QTextStream str(&res);
    QString description = ticket->fieldByName("description").toString();
    if (description.length() > 256)
        description = QString("%1...").arg(description.left(256));

    str << "<html><body>"
        << "<p><em>" << ticket->fieldByName("summary").toString() << "</em></p>"
        << "<small>by " << ticket->fieldByName("reporter").toString() << " at "
        << ticket->created().toLocalTime().toString() << "</small>"
        << "<p><pre>" << description << "</pre></p>"
        << "</body></html>";

    return res;
}






